import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/model/SPClassPointsOrderListEntity.dart';
import 'package:hongsheng/pages/common/SPClassNoDataView.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassToastUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:hongsheng/widgets/SPClassBallFooter.dart';
import 'package:hongsheng/widgets/SPClassBallHeader.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';


class SPClassExchangeItemPage extends StatefulWidget {
  final String ?type;
  const SPClassExchangeItemPage({Key? key,this.type}) : super(key: key);

  @override
  _SPClassExchangeItemPageState createState() => _SPClassExchangeItemPageState();
}

class _SPClassExchangeItemPageState extends State<SPClassExchangeItemPage>with AutomaticKeepAliveClientMixin {
  EasyRefreshController _controller =new EasyRefreshController();
  List<SPClassPointsOrderEntity> dataList =[];
  int page =1;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      firstRefresh: true,
      controller: _controller,
      header:SPClassBallHeader(
          textColor: Color(0xFF666666)
      ),
      footer: SPClassBallFooter(
        textColor: Color(0xFF8F8F8F),
      ),
      onLoad: spFunOnMore,
      onRefresh:spFunOnRefresh,
      emptyWidget: dataList.isEmpty?SPClassNoDataView(height: width(400),):null,
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate(
                (context, index) {
              return itemWidget(dataList[index]);
            },
            childCount: dataList.length,
          ),
        ),
      ],
    );
  }
  
  Widget itemWidget(SPClassPointsOrderEntity data){
    return Container(
      margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
      padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(12)),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Image.network(
                data.spProIcon!,
                fit: BoxFit.cover,
                width: width(77),
              ),
              SizedBox(width: width(12),),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        Container(
                          width: width(44),
                          height: width(17),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: data.spProStatus=='1'?Colors.red:Color(0xFF2BD99F),
                              borderRadius: BorderRadius.circular(150)
                          ),
                          child: Text(data.spProStatus=='1'?'待发货':'已发货',style: TextStyle(color: Colors.white,fontSize: sp(10),),),
                        )
                      ],
                    ),
                    Text(data.spProProductName??'',style: TextStyle(fontSize: sp(17),),),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text('${data.spProPrice}积分',style: TextStyle(fontSize: sp(13),color: MyColors.main1),),
                        Text('x1',style: TextStyle(fontSize: sp(13),color: MyColors.grey_99),),
                      ],
                    ),
                  ],
                ),
              )
            ],
          ),
          SizedBox(
            height: width(10),
          ),
         data.spProProductType=='1'? Column(
           crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              data.spProExpressSn==null?Container():Row(
                children: <Widget>[
                  Expanded(
                    child: Text('   快递号:${data.spProExpressSn}',style: TextStyle(fontSize: sp(13),),),
                  ),
                  SizedBox(
                    width: width(10),
                  ),
                  GestureDetector(
                    onTap: (){
                      try {
                        ClipboardData value =
                        new ClipboardData(
                          text: data.spProExpressSn,
                        );
                        Clipboard.setData(value);
                        SPClassToastUtils.spFunShowToast(
                            msg: "复制成功");
                      } on PlatformException {
                        SPClassToastUtils.spFunShowToast(
                            msg: "复制失败");
                      }
                    },
                    child: Text('复制',style: TextStyle(fontSize: sp(13),),),
                  )
                ],
              ),
              Text('   收货人:${data.spProRealName}  ${data.spProPhone}',style: TextStyle(fontSize: sp(13),),),
              Text('收货地址:${data.spProAddress}',style: TextStyle(fontSize: sp(12),color: MyColors.grey_66),),
            ],
          ):Text('   手机号:${data.spProPhone}',style: TextStyle(fontSize: sp(13),),),
        ],
      ),
    );
  }

  Future<void>  spFunOnRefresh() async {
    page =1;
    SPClassApiManager.spFunGetInstance().spFunPointsOrderList<SPClassPointsOrderListEntity>(params:{"page":page.toString(),"status":widget.type},spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          dataList =result.spProList!;
          setState(() {
          });
        }
    ));
  }

  Future<void>  spFunOnMore() async {
    page ++;
    SPClassApiManager.spFunGetInstance().spFunPointsOrderList<SPClassPointsOrderListEntity>(params:{"page":page.toString(),"status":widget.type},spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          dataList.addAll(result.spProList!);
          setState(() {
          });
        }
    ));
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}
